package com.apop.demo.encrypt.impl;

import com.apop.demo.encrypt.EncryptionStrategy;
import com.apop.demo.encrypt.pojo.DecodeDataDto;
import com.apop.demo.encrypt.pojo.EncodeDataDto;
import com.apop.demo.encrypt.util.FzSMEncryptUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

/**
 * 中信SM4加密方案
 *
 * @Description:
 * @Author: zjy
 * @Date: 2025/2/26 15:17
 */
@Component("fzsm")
@Slf4j
public class FzSMEncryptionStrategy implements EncryptionStrategy {

    @Override
    public String encrypt(Object data) {
        EncodeDataDto encodeDataDto = (EncodeDataDto) data;
        try {
            log.info("执行fzsm4加密策略...");
            log.info("原始报文：{}", encodeDataDto.getOriginalData());
            String encData = FzSMEncryptUtil.encode(encodeDataDto);
            log.info("加密报文：{}", encData);
            return encData;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String decrypt(Object data) {
        DecodeDataDto decodeDataDto = (DecodeDataDto) data;
        try {
            log.info("执行fzsm4解密策略...");
            log.info("原始报文：{}", decodeDataDto.getEncodeData());
            String plainData = FzSMEncryptUtil.decode(decodeDataDto);
            log.info("解密报文：{}", plainData);
            return plainData;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Boolean verifySignWithPublicKey(String original, String sign, String publicKey) {
        try {
            long start = System.currentTimeMillis();
            Boolean result = FzSMEncryptUtil.validateSign(original, sign, publicKey);
            log.info("验签执行耗时：{} ms", System.currentTimeMillis() - start);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
